"""
SKU管理视图
"""
from meiduo_admin.serializers.sku_serializers import *
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from goods.models import SKU, GoodsCategory, SPU, SPUSpecification, SKUSpecification
from meiduo_admin.paginations import MyPage
from django.db.models import Q


class SKUGoodsView(ModelViewSet):
    """
    SKU增删改查
    """
    queryset = SKU.objects.all()
    serializer_class = SKUGoodsSerializer
    pagination_class = MyPage

    def get_queryset(self):
        # 1、获取查询字符串参数keyword --> request.query_params.get("keyword")
        keyword = self.request.query_params.get('keyword')
        # 2.过滤
        if keyword:
            return self.queryset.filter(Q(name__contains = keyword) | Q(caption__contains=keyword)) # 过滤出用户名包含关键词的查询集
        else:
            return self.queryset.all() # .all()获取最新数据
    


class SKUCategorieView(ListAPIView):
    """
    SKU可选分类序列化返回视图
    """
    queryset = GoodsCategory.objects.all()
    serializer_class = SKUCategorieSerializer

    def get_queryset(self):
        # 方法1
        # categorys1 = self.queryset.filter(parent_id=None)
        # category3_list = []
        # for category1 in categorys1:
        #     categorys2 = self.queryset.filter(parent_id=category1.id)
        #     for category2 in categorys2:
        #         categorys3 = self.queryset.filter(parent_id=category2.id)
        #         for category3 in categorys3:
        #             category3_list.append({
        #                 'id': category3.id,
        #                 'name': category3.name
        #             })
        # return category3_list
        # 方法1的优化
        categorys1 = self.queryset.filter(parent_id=None)
        categorys2_list = []
        categorys3_list = []
        for category1 in categorys1:
            categorys2 = self.queryset.filter(parent_id=category1.id)
            for category2 in categorys2:
                categorys2_list.append(category2.id)
        for category2_id in categorys2_list:
            categorys3 = self.queryset.filter(parent_id=category2_id)
            for category3 in categorys3:
                categorys3_list.append({
                    'id': category3.id,
                    'name': category3.name
                })
        return categorys3_list
                
        # 老师的方法
        # return self.queryset.filter(
        #     parent_id__gt = 37 # 父级的id大于37的是三级分类
        # )


class SPUSimpleView(ListAPIView):
    """
    SPU可选列表数据视图
    """
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

class SPUSpecView(ListAPIView):
    """
    SPU可选规格和选项
    """
    queryset = SPUSpecification.objects.all()
    serializer_class = SPUSpecSerialzier
    # 因为我们继承的是ListAPIView，在拓展类中是通过get_queryset获取数据，但是我们现在要获取的是规格信息，所以重写get_queryset
    def get_queryset(self):
        # 获取spu的id值就是路径中的pk,----如何在非视图函数获取路径参数？
        # self.kwargs封装了路径参数（字典）
        pk=self.kwargs.get('pk')
        # 根据spu的id值关联过滤查询出规格信息
        return self.queryset.filter(spu_id=pk)

